﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.SqlClient;

namespace HXDNB.DAL
{
    public class BuyParentBLL : BaseDAL<BuyParentOrder>
    {
        public BuyParentBLL() : base("BuyParentOrder") { }


        public IQueryable<BuyParentList> GetList(DateTime? OrderDate, int Customer, int Product, int State)
        {
            var list = from i in _e.BuyParentOrder
                       join j in _e.Customer on i.CustomerID equals j.ID into Temp
                       from t in Temp.DefaultIfEmpty()
                       where (Customer < 0 || i.CustomerID == Customer)
                       && (OrderDate == null || i.OrderTime == OrderDate)
                       select new BuyParentList
                       {
                           CreateTime = i.CreateTime,
                           CustomerID = i.CustomerID,
                           CustomerName = t.Name,
                           ID = i.ID,
                           KoreaRate = i.KoreaRate,
                           OrderTime = i.OrderTime,
                           Remark = i.Remark,
                           State = i.State
                       };
            if (Product > 0 || State > 0)
            {
                list = (from i in list
                        join j in _e.BuyOrder on i.ID equals j.ParentID into Temp
                        from t in Temp.DefaultIfEmpty()
                        where (Product < 0 || t.ProductID == Product) && (State < 0 || t.State == State)
                        select i).Distinct();
            }
            var result = from i in list
                         orderby i.CreateTime descending
                         select i;
            return result;
        }
    }
}
